#include "dfs.h"
#include "dfs_inner.h"
#include "./romfs/romfs.h"

#include "os_sal.h"

#include "drv_sdcard.h"


/* 虚拟文件系统-用户应用层 */

/* define */

#define TAG "DFS-USER"


/* static var */

static const char dfs_romfs_root_readme_txt[] = "This is a readme.txt file in ROMFS";

static const struct romfs_dirent dfs_romfs_root_tbl[] = {
    {
        .type = ROMFS_DIRENT_DIR, 
        .name = "flash", 
        .data = NULL, 
        .size = 0
    },
    {
        .type = ROMFS_DIRENT_DIR, 
        .name = "sd", 
        .data = NULL, 
        .size = 0
    },
    {
        .type = ROMFS_DIRENT_DIR, 
        .name = "udisk", 
        .data = NULL, 
        .size = 0
    },
    {
        .type = ROMFS_DIRENT_FILE,
        .name = "readme.txt",
        .data = (const uint8_t *)dfs_romfs_root_readme_txt,
        .size = sizeof(dfs_romfs_root_readme_txt),
    }
};

static const struct romfs_dirent dfs_romfs_root = {
    .type = ROMFS_DIRENT_DIR,
    .name = "/", 
    .data = (const uint8_t *)dfs_romfs_root_tbl, 
    .size = (sizeof(dfs_romfs_root_tbl) / sizeof(dfs_romfs_root_tbl[0]))
};



static os_task_handle_t user_diskio_task_handler = NULL;





/* static func */


#include "ff.h"

void fs_test(void)
{
    FATFS fs;
    // FIL file;
    DIR DirInf;
    FILINFO FileInf;
    FILINFO fno;
    FRESULT ret;
    uint32_t cnt = 0;

    ret = f_mount(&fs, "1:/", 0);
    if (ret != FR_OK)
    {
        printf("mount fail\r\n");
        return;
    }

    printf("opendir...");
    ret = f_opendir(&DirInf, "1:/");
    if (ret != FR_OK)
    {
        printf("opendir fail(%d)\r\n", ret);
        return;
    }

    printf("OK\r\n");
    printf("属性        |  文件大小 | 短文件名 | 长文件名\r\n");
    for (cnt = 0;; cnt++)
    {
        ret = f_readdir(&DirInf, &FileInf); /* 读取目录项，索引会自动下移 */
        if (ret != FR_OK || FileInf.fname[0] == 0)
        {
            break;
        }

        if (FileInf.fname[0] == '.')
        {
            continue;
        }

        /* 判断是文件还是子目录 */
        if (FileInf.fattrib & AM_DIR)
        {
            printf("(0x%02d)目录  ", FileInf.fattrib);
        }
        else
        {
            printf("(0x%02d)文件  ", FileInf.fattrib);
        }

        f_stat(FileInf.fname, &fno);

        /* 打印文件大小, 最大4G */
        printf(" %10d", (int)fno.fsize);

        printf("  %s\r\n", (char *)FileInf.fname); /* 长文件名 */
    }

    f_mount(NULL, "1:/", 0);
}


static void user_sd_mount(void)
{
    int sta = 0;

    sta = dfs_fs_mount("sd", "/sd", "fatfs", 0, NULL);
    if (sta == 0)
    {
        LOG_I(TAG, "'/sd' mount OK");
    }
    else
    {
        LOG_E(TAG, "'/sd' mount failed!");
    }
}

static void user_sd_unmount(void)
{
    os_delay(100);
    dfs_fs_unmount("/sd");
}





static void user_diskio_task(void *param)
{
    uint8_t flag_sd = 0;
    uint8_t tmp;

    os_delay(100);

    // fs_test();

    while (1)
    {
        os_delay(250);

        tmp = drv_sdcard_is_exist();
        if (flag_sd == 0 && tmp == 1)
        {
            // LOG_I(TAG, "sd mount");
            user_sd_mount();
        }
        else if (flag_sd == 1 && tmp == 0)
        {
            // LOG_I(TAG, "sd unmount");
            user_sd_unmount();
        }
        flag_sd = tmp;

    }
    
}


/* export func */

int dfs_user_init(void)
{
    os_status_t sta;

    /* filesystem register - romfs : 创建虚拟目录以挂载其他磁盘 */
    romfs_dfs_register();

    /* root dfs mount by romfs */
    if (dfs_fs_mount("root", "/", "rom", 0, &(dfs_romfs_root)) != 0)
    {
        LOG_E(TAG, "rom mount to '/' failed!");
    }


    /* filesystem register - fatfs*/
    {
        extern int ff_port_dfs_register(void);
        ff_port_dfs_register();
    }


    /* spi-flash dfs mount */


    /* sd-card dfs mount */
    sta = os_task_create(user_diskio_task, NULL, "dfs_detect", 256, 10, (os_task_handle_t)&user_diskio_task_handler);
    if (sta != OS_STA_SUCCESS)
    {
        LOG_E(TAG, "dfs_detect task create fail");
    }

    return 0;
}













